<template>
  <view>
    <view class="goods-list">
      <view v-for="(item, index) in goodsList" :key="index" @click="gotoDetail(item)">
        <my-goods :goods="item"></my-goods>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: 'GoodsListPage',
  components: {},
  data() {
    return {
      // 请求参数对象
      queryObj: {
        // 查询关键词
        query: '',
        // 商品分类Id
        cid: '',
        // 页码值
        pagenum: 1,
        // 每页显示多少条数据
        pagesize: 10
      },
      goodsList: [],
      totaol: 0,
      isloading:false //在加载中
    };
  },
  created() {},
  mounted() {},
  onLoad(options) {
    console.log(options);
    Object.assign(this.queryObj, options);
    this.getGoodsList()
  },
  onReachBottom() {
    // 阻止频繁发请求,阻止已经结束还发请求
    if(this.loading||(this.total === this.goodsList.length && this.total !==0)) return
    this.queryObj.pagenum++
    this.getGoodsList()
  },
  async onPullDownRefresh() {
    // 1.清空数据
    this.queryObj.pagenum=1
    this.goodsList=[]
    this.total=0
    this.isloading=false
    // 2.发请求
    await this.getGoodsList()
    // 3.关闭下拉提示
    uni.stopPullDownRefresh()
  },
  methods: {
    gotoDetail(item){
      uni.navigateTo({
        url:'/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id
      })
    },
    async getGoodsList() {
      this.isloading=true
      let {
        data: {
          message: { goods, total },
          meta: { status }
        }
      } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj);
      this.isloading = false
      if (status !== 200) return uni.$showMsg();
      this.goodsList = [...this.goodsList,...goods];
      this.total = total;
    }
  }
};
</script>

<style lang="scss">

</style>
